#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <signal.h>
#include <pthread.h>
#include <linux/unistd.h>
#include <unistd.h>


#define SPORT     10514
#define MNG_PORT  41502
#define MAXLOG 1
#define MAX_PACKET 120
#define LOCAL_SOCK "/tmp/hplogd.sock"

char syslog[] = 
{
};
int usage()
{
    printf("\nDescription: the manager of the hplog server\n");
    printf("Usage: command srvaddr action parameter\n");
    printf("                       action:\n");
    printf("                            <1-update the policy file>  <parameter- the path of the policy file>\n");
    printf("                            <2-reopen log file>         <parameter- the path of the new file>\n");
    printf("                            <3-reopen color file>       <parameter- the path of the new file>\n");
    printf("For example: srvmng 10.1.136.8 1 \"/data/kevenzhu/policy_20100716.conf\"\n");
    printf("             srvmng 10.1.136.8 2 \"/data/kevenzhu/file_20100716.log\"\n");
    printf("             srvmng 10.1.136.8 3 \"/data/kevenzhu/color_20100716.log\"\n\n");
}

int get_policy_version(char* pathname)
{
    struct stat file_status;
    char   *buff,*tmp_buff,*tmp_msg_buff;
    int    icounter,fd;

    if(stat(pathname,&file_status))
    {
        return 0;
    }
    buff = (char *)malloc(file_status.st_size);
    if(!buff)
    {
        return 0;
    }

    /*during the update stage, new connected client will NOT be notified!!!!!!*/
    if(-1 == (fd = open(pathname,O_RDONLY)))
    {
        free(buff);
        return 0;
    }
    tmp_buff = buff;
    while((icounter = read(fd,tmp_buff,SSIZE_MAX))>0)
    {
        tmp_buff = tmp_buff + icounter;
    }
    
    /*close the policy file*/
    close(fd);

    /*read the version from the file*/
    /*now we get all of the msg in the buff*/
    tmp_msg_buff = strstr(buff,"version");
    if(tmp_msg_buff)
    {
        tmp_msg_buff = tmp_msg_buff + 7;//strlen("version")
        tmp_msg_buff = strstr(tmp_msg_buff,"version");
        if(tmp_msg_buff)
        {
            while(*tmp_msg_buff != '=')
            tmp_msg_buff++;
            tmp_msg_buff++;
            return atoi(tmp_msg_buff);
        }
        else
            return 0;

    }
    else
        return 0;
}

void* draw_line(void *argv)
{
    printf("*");
    usleep(100000);
}
int main(int argc, char *argv[])
{
    int sockfd,counter,ipos,idebug;
    char buff[MAX_PACKET];
    //struct hostent *host;
    struct sockaddr_in serv_addr;
    char *tmp_buff;
    int  iversion;
    struct timeval tv;
    pthread_t idle_t;

    /*host = gethostent();
    if(NULL == host)
    {
        printf("error to get the hostent %d \n",errno);
        exit(1);
    }*/
    if(argc != 4)
    {
        usage();
        return 1;
    }
    tmp_buff  = (char*)malloc(2 + strlen((((char*)argv[3]))));
    if(!tmp_buff)
    {
        printf("failed to get the memory \n");
        return 1;
    }
    tmp_buff[0] = atoi(argv[2]);
    tmp_buff[1] = strlen((((char*)argv[3])));
    strcpy(tmp_buff+2,((((char*)argv[3]))));
    /*update policy*/
    printf("it's %d len %d\npath %s\n",atoi(argv[2]),strlen((((char*)argv[3]))),((((char*)argv[3]))));
    //iversion = get_policy_version(argv[2]);

    sockfd = socket(AF_INET,SOCK_STREAM,0);
    //sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        printf("error to create the socket, error %d\n",errno);
        exit(1);
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port   = htons(MNG_PORT);  
    inet_aton(argv[1],&serv_addr.sin_addr.s_addr);

    /*set the recv timeout value*/
    tv.tv_sec = 2;
    tv.tv_usec= 0;
    setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv));    

    if(connect(sockfd, (struct sockaddr*)&serv_addr,sizeof(struct sockaddr))== -1)
    {
        perror("failed to connect socket");
        printf("error to call the connect %d\n",errno);
        exit(1);
    }
    if(-1 == send(sockfd,tmp_buff,2 + strlen((((char*)argv[3]))),0))
    {
        printf("error to send the traffic %d \n",errno);
        exit(1);
    }
    printf("I'm ready to work\n");
    pthread_create(&idle_t,NULL,draw_line,NULL);
    ipos = recv(sockfd,tmp_buff,2,0);
    printf("\n");
    pthread_cancel(idle_t);
    if(ipos > 0)
    {
        printf("response %d\n",tmp_buff[0]);
        if(*tmp_buff == 4)
        {
            printf("Accept the request, It's OK\n");
        }
        else
        {
            printf("The request has been rejected, sorry\n");
        }
    }
    else
    {
        printf("Failed to get response from the server\n ");
    }
    close(sockfd);
    free(tmp_buff);
    return 1;
}

